
#
# oeischek.py -- Simple program to superficially check OEIS-submissions,
#                and create a HTML-check file.
# Written January 15 2007 and placed in Public Domain.
#
# Usage:
# from oeischek import *

# oeischek('seqs50-.txt') # (for example...)


import os
import re


def readbfile_editfile(Anum):
  '''Open b-file's edit-file for reading and construct a string from its contents.'''
  filename = "a" + Anum[1:] + ".edit"

  try:
    infp = open(filename,'r')
  except IOError: # There were no edit-file present.
    return(None)

  lines = ""

  for line in infp.xreadlines(): 
    lines = lines + line

  return(lines + "\n") # Add a newline.


def oeischek(filename):
  '''Opens file "filename" for reading and similar file with extension ".htm" for writing.'''
  oeischek_aux(filename,False)

def oeischek_aux(filename,add_editlines):
  '''Opens file "filename" for reading and similar file with extension ".htm" for writing. Give the second argument as True if you want to add edit-lines for b-files.'''
  infp = open(filename,'r')
  try:
    lastdot = filename.rindex('.')
    outfilename = filename[:lastdot] + ".htm"
  except ValueError: # There were no dot in the filename
    outfilename = filename + ".htm"

  outfp = open(outfilename,'w')

  outfp2 = None

  if(add_editlines):
    outfilename2 = filename + ".txt"
    outfp2 = open(outfilename2,'w')

  outfp.write("<html><head><title>Check " + filename +"</title></head><body><pre>\n")

# linepat = re.compile(r'^%(.) (A[0-9]+) (.*)')
# linepat = re.compile(r'^%(.) (A[0-9]+) (*)')
  linepat = re.compile(r'^%(.) (A[0-9]+)')
  termspat = re.compile(r'^ ([0-9]+),([0-9]+),(.*)')
  cfpat = re.compile(r'.*[Cc]\.[Ff]')
  ikifipat = re.compile(r'.*[Ii][Kk][Ii]\.[Ff][Ii]')
  gatopat = re.compile(r'.*[Gg]atomor')

  cur_anum = ""
  some_H_line_already_encountered = False
  added_n_editlines = 0

  for line in infp.xreadlines(): 
    m = linepat.match(line)
    if(m):
      key  = m.group(1) #
      anum = m.group(2) #
      contents = m.string[m.end(2):] # The rest

# Catch a few of my idiosyncracies:

      cfm = cfpat.match(contents)
      if(cfm):
        print 'ERROR: "Confer" should be abbreviated as "Cf." not as "c.f" ! On line:\n' + line

      ikifim = ikifipat.match(contents)
      if(ikifim):
        print 'ERROR: @iki.fi -address should be replaced with @gmail.com address:\n' + line

      gatom = gatopat.match(contents)
      if(gatom):
        print 'ERROR: Use proper terms (Catalan automorphisms!):\n' + line

#     print "key=" + key + ":anum=" + anum + ":cur_anum=" + cur_anum + ":"

      if('I' == key):
        some_H_line_already_encountered = False
        if(anum == cur_anum):
          print "ERROR: Non-unique A-number on the line:\n" + line

        cur_anum = anum
      elif(anum != cur_anum):
        if("" == cur_anum):
          print "ERROR: Missing %I-line (or superfluous lines) before the line:\n" + line
        else:
          print "ERROR: Out of place A-number (expected " + cur_anum + ") on the line:\n" + line

      if('S' == key):
        m2 = termspat.match(contents)
        if(m):
          t1   = m2.group(1) #
          t2   = m2.group(2) #
          rest = m2.group(3) #
          outfp.write('%' + key + ' ' + anum + ' ' + t1 + ',' + t2 + ','
                      + '<A HREF="http://www.research.att.com/~njas/sequences/?q=' + rest + '">'
                      + rest + '</A>' + '\n')
        else:
          print "ERROR: Badly formatted %S-line (at least 4 terms are required):\n" + line
          outfp.write(line)

      elif((None != outfp2) and ('H' == key) and (not some_H_line_already_encountered)):
        some_H_line_already_encountered = True
        editline = readbfile_editfile(anum)
        if(None != editline):
          outfp.write(editline)
          outfp2.write(editline)
          added_n_editlines += 1
        outfp.write(line)
      else: outfp.write(line)
    else:
      outfp.write(line) # It's probably an empty line between.
      cur_anum = ""
    if(None != outfp2): outfp2.write(line)


  outfp.write("\n</pre><h3>Added " + str(added_n_editlines)
               + " %H-lines for b-files.</h3></body></html>\n")
  outfp.close()
  if(None != outfp2): outfp2.close()




# This module ends here.

